/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the Common Development
 * and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at
 * src/com/vodafone360/people/VODAFONE.LICENSE.txt or
 * http://github.com/360/360-Engine-for-Android
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each file and
 * include the License file at src/com/vodafone360/people/VODAFONE.LICENSE.txt.
 * If applicable, add the following below this CDDL HEADER, with the fields
 * enclosed by brackets "[]" replaced with your own identifying information:
 * Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 * Copyright 2010 Vodafone Sales & Services Ltd.  All rights reserved.
 * Use is subject to license terms.
 */

package com.vodafone.nowplus.android.peoplemail.singlesignon;

import java.io.InputStream;
import java.net.URL;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;

import android.app.Activity;
import android.content.res.AssetManager;
import android.os.AsyncTask;
import android.util.Log;

public class CertificateReader {
    
    private static java.security.KeyStore mKeyStore;

    // password for PKCS#12 certificate file    
    private final static String mSecret = "#######";

    // filename of the certificate in assets folder
    private final static String mCertificateFileName = "PAMC_Client.p12";

    private X509Certificate mCertificate;
    
    // alias of the certificate
    private static final String mAlias="PAMC Client";
    
    private static CertificateReader instance;

    /**
     * Constructor of the certificate reader. Reads a PKCS#12 encrypted file and
     * extracts the private key and the certificate
     * 
     * @param activity
     */
    private CertificateReader(Activity activity) {
        try {
            AssetManager asset = activity.getAssets();
            InputStream is = asset.open(mCertificateFileName);
            mKeyStore = java.security.KeyStore.getInstance("PKCS12");
            mKeyStore.load(is, mSecret.toCharArray());
            mCertificate = (X509Certificate)mKeyStore.getCertificate(mAlias);

        } catch (Exception e) {

            Log.e("CertificateReader","Constructor", e);
        }
    }

    /**
     * Singleton instance getter for the Certificate reader
     * The activity has to be passed to the constructor of this singleton object
     * to gain access to the asset folder where the certificates resides.
     * 
     * @param activity
     * @return
     */
    public static synchronized CertificateReader getInstance(Activity activity) {
        if (instance == null) {
            instance = new CertificateReader(activity);
        }
        return instance;
    }

    /**
     * Gets the certificate
     * 
     * @return the certificate
     */
    public X509Certificate getCertificate() {

        return mCertificate;

    }

    /**
     * Gets the private key
     * 
     * @return the private key of the certificate
     */
    public PrivateKey getPrivateKey() {
        try {
            return (PrivateKey)mKeyStore.getKey(mAlias, mSecret.toCharArray());
        } catch (Exception e) {
            Log.e("CertificateReader","getPrivateKey", e);
            return null;
        }
    }
    

    
    
}
